import 'dart:collection';
import 'dart:io';
import 'package:flutter/services.dart';
import 'dart:async';
import 'package:safe_local_storage/safe_local_storage.dart';
abstract class OhosAssetLoader {
   final MethodChannel methodChannel = const MethodChannel('media_kit');
 
   static final HashMap<String, String> _loaded = HashMap<String, String>();
   static final HashMap<String, String> _assets = HashMap<String, String>();
  // static  final  delay = ControllableDelay();
  static loadSync(String asset)  {
        final lookup = _loaded[asset];
       
        if (lookup != null) {
          return  Directory.systemTemp.path+lookup;
        }
        
        _loaded[asset] = asset;
        _assets['asset'] = asset;
        _copyAssetToFilesDir(asset);
        
       
        return Directory.systemTemp.path+asset;
  }
  static awaitOhos()async {

      await Future.delayed(Duration(milliseconds: 100));
    
      
       
  }
  static Future<String> _copyAssetToFilesDir(String asset) async {
    
    final MethodChannel methodChannel = const MethodChannel('media_kit');
    final path=await methodChannel.invokeMethod("copyAssetToFilesDir",asset);
    // timer.cancel();
    if (!File(asset).existsSync_()) {
          throw Exception('Unable to load asset: $asset');
        }
     
    return path;
  }
}

